#include <QDir>
#include <QPainter>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newgamedialog.h"
#include "gamesummarydialog.h"
#include "gamesettings.h"
#include "../GameEngine/engine.h"
#include "../GameEngine/bullet.h"
#include "../GameEngine/minion.h"
#include "../GameEngine/map.h"
#include "../GameAI/gameai.h"
#include "../GameInterface/networkinterface.h"
#include "../GameInterface/gameinterface.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    resize(400,400);

    NewGameWindow = new NewGameDialog(this);
    NewGameWindow->setHidden(true);

    MinionSelectionWindow = new MinionSelectionDialog(this);
    MinionSelectionWindow->setHidden(true);

    GameSummaryWindow = new GameSummaryDialog(this);
    GameSummaryWindow->setHidden(true);

    QDir::setCurrent(QApplication::applicationDirPath());

    Engine::TicksPerSecond = 40;

    connect(&GameTimer,SIGNAL(timeout()), this, SLOT(Tick()));
    GameTimer.setInterval(1000 / MaxTicksPerSecond);
}

void MainWindow::ShowMainMenu()
{
    ui->NewGameButton->setVisible(true);
    ui->BrowseButton->setVisible(true);
    ui->QuickPlayButton->setVisible(true);
    ui->PlayerName->setVisible(true);
    ui->labelPlayerName->setVisible(true);
}

void MainWindow::PrepareGame(GameSettings &Settings)
{
    this->resize(800,700);

    ui->NewGameButton->setHidden(true);
    ui->BrowseButton->setHidden(true);
    ui->QuickPlayButton->setHidden(true);
    ui->PlayerName->setHidden(true);
    ui->labelPlayerName->setHidden(true);

    MinionSelectionWindow->Initialize(Settings);

    CurrentGameSettings = Settings;
}

void MainWindow::StartGame(GameSettings &Settings)
{
    Engine::LoadMap("Maps/map.txt");

    for(int team = 0; team < Settings.Teams.count(); ++team)
    for(int slot = 0; slot < Settings.Teams[team].count(); ++slot)
    {
        if(!Settings.Teams[team][slot].MinionName.isEmpty())
        {
            double x,y;
            Engine::GetRespawnPoint(team + 1, x, y, team == 0 ? slot : 6 - slot);

            QString MyMinionFile = QString("Minions/%1/%1.txt").arg(Settings.Teams[team][slot].MinionName);
            Minion *MyMinion = Engine::SpawnMinion(MyMinionFile,
                                        team + 1, x, y, 0.0,
                                        Settings.Teams[team][slot].Name);
            MyMinion->Spawn();

            Settings.Teams[team][slot].Interface->SetMyMinion(MyMinion);
            Settings.Teams[team][slot].Interface->GameStarted(team*6 + slot,
                        QPair<int,int>(Engine::CurrentMap->GetBackground()->width(),
                                       Engine::CurrentMap->GetBackground()->height()));

            if(team == Settings.MyTeam && slot == Settings.MyPosition)
            {
                MyInterface = (GameInterface*)Settings.Teams[team][slot].Interface;
                MyInterface->setParent(this);
                MyInterface->setVisible(true);
                MyInterface->resize(700, 700);
            }
        }
    }

    TicksCounter = 0;

    Engine::GameRunning = true;
    GameTimer.start();
}

void MainWindow::Tick()
{
    TicksCounter++;
    if(TicksCounter >= MaxTicksPerSecond) TicksCounter = 0;

    if(TicksCounter % TicksAI == 0) GameAI::Tick();

    Engine::Tick();

    MyInterface->repaint();
    repaint();

    if(!Engine::GameRunning) EndGame();
}


void MainWindow::EndGame()
{
    GameTimer.stop();
    for(int team = 0; team < CurrentGameSettings.Teams.count(); ++team)
    for(int slot = 0; slot < CurrentGameSettings.Teams[team].count(); ++slot)
    {
        delete CurrentGameSettings.Teams[team][slot].Interface;
        CurrentGameSettings.Teams[team][slot].Interface = nullptr;
    }

    Engine::EndGame();

    resize(400,400);
    GameSummaryWindow->DisplayStatistics();
    GameSummaryWindow->setVisible(true);
}

QString MainWindow::GetPlayerName() { return ui->PlayerName->text(); }

void MainWindow::paintEvent(QPaintEvent *)
{
    if(Engine::GameRunning)
    {
        QPainter painter(this);

        painter.drawText(710, 10, "Player1");
        painter.drawText(710, 20, "Level: " +
                         QString::number(MyInterface->GetMyMinion()->GetLevel()));
        painter.drawText(710, 30, "Exp: " +
                         QString::number(MyInterface->GetMyMinion()->GetExperience()));
        painter.drawText(710, 40, "Health: " +
                         QString::number((int)MyInterface->GetMyMinion()->GetHealth()) + " / " +
                         QString::number((int)MyInterface->GetMyMinion()->GetMaxHealth()));
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_NewGameButton_clicked()
{
    NewGameWindow->exec();
}
